package com.hx.springboot1.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hx.springboot1.dao.ScoreDao;
import com.hx.springboot1.pojo.dto.ScoreDTO;
import com.hx.springboot1.pojo.dto.ScoreReportDTO;
import com.hx.springboot1.pojo.entity.TabCourse;
import com.hx.springboot1.pojo.entity.TabScore;
import com.hx.springboot1.service.ScoreService;
import com.hx.springboot1.utils.ResultMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Author: hx
 * Date: 2021-05-25 20/21
 * Description: Nothing else
 */
@Service
@Slf4j
public class ScoreServiceImpl implements ScoreService {
    @Autowired
    ScoreDao scoreDao;
    @Override
    public PageInfo selectAllScore(String searchName, String pageNum, String pageSize) {
        List<ScoreDTO> allScore;
        int pn = 1;
        int ps = 10;
        if (!StringUtils.isEmpty(pageNum)) {
            pn = Integer.parseInt(pageNum);
        }
        if (!StringUtils.isEmpty(pageSize)) {
            ps = Integer.parseInt(pageSize);
        }
        //   PageHelper  要在查询之前使用
        PageHelper.startPage(pn, ps);
        if (StringUtils.isEmpty(searchName)) {
            allScore = scoreDao.selectAllScore();
            log.info("用户{}点击scoreList页面", SecurityContextHolder.getContext().getAuthentication().getName());
        } else {
            searchName = "%" + searchName + "%";
            allScore = scoreDao.selectAllScoreByStuName(searchName);
            log.info("{}查询了学生：{}的成绩信息", SecurityContextHolder.getContext().getAuthentication().getName(), searchName);
        }
        PageInfo<ScoreDTO> pageInfo = new PageInfo<>(allScore);
        return pageInfo;
    }

    @Override
    public ResultMsg insertOrUpdateScore(Integer scoreId,Integer stuId,String stuName,Integer courseId,Integer scoreTime,Integer scoreScore) {
        int effectRows = 0;
        if (scoreId==null&&stuId!=null&&courseId!=null&&scoreTime!=null&&scoreScore!=null){
            effectRows += scoreDao.insertScore(stuId, courseId, scoreTime, scoreScore);
        }else if (scoreId!=null&&scoreTime!=null&&scoreScore!=null&&scoreScore>=0&&scoreScore<=100){
            effectRows += scoreDao.updateScore(scoreTime,scoreScore,scoreId);
        }else {
            return new ResultMsg("数据输入有误！",false);
        }
        if (effectRows>0){
            return new ResultMsg("操作成功！",true);
        }else {
            return new ResultMsg("操作失败！",false);
        }

    }

    @Override
    public TabScore selectScoreByScoreId(Integer scoreId) {
        if (scoreId!=null){
            TabScore tabScore = scoreDao.selectScoreByScoreId(scoreId);
            return tabScore;
        }
        return null;
    }

    @Override
    public ResultMsg delScore(Integer scoreId) {
        int effectRows = 0;
        if (scoreId!=null){
            effectRows += scoreDao.delScoreByStuId(scoreId);
        }
        if (effectRows>0){
            return new ResultMsg("删除成功！",true);
        }else {
            return new ResultMsg("删除失败！",false);
        }
    }
    @Override
    public PageInfo selectScoreReport(Integer yearId,Integer pageNum,Integer pageSize) {
        int pn = 1;
        int ps = 10;
        List<ScoreReportDTO> report;
        if (pageNum!=null){
            pn = pageNum;
        }
        if (pageSize!=null){
            ps = pageSize;
        }
        PageHelper.startPage(pn,ps);
        if (yearId==null){
            report = scoreDao.selectAllReport();
        }else {
            report = scoreDao.selectReportByYearId(yearId);
        }
        PageInfo<ScoreReportDTO> pageInfo = new PageInfo<>(report);
        return pageInfo;
    }
}
